//
//  SYComposePictureView.swift
//  SYWeibo
//
//  Created by Eric on 16/7/27.
//  Copyright © 2016年 Eric. All rights reserved.
//

import UIKit
//cell 的可重用标示符
private let SHComposePictureViewCellId = "SHComposePictureViewCellId"
class SYComposePictureView: UICollectionView {
    
    //定义一个闭包 通知vc弹出相册
    var closure:(()->())?
    
    //保存图片数组
    var imageList: [UIImage] = [UIImage]()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //MARK :  -添加图片  让外界调用
    func addImages(image: UIImage){
        //添加图片
        imageList.append(image)
        //刷新 UI
        reloadData()
    }
    
    // MARK: -  设置视图
    private func setupUI(){
        backgroundColor = UIColor.clearColor()
        let cellMargin: CGFloat = 5
        let cellWH = (SCREENW - 20 - cellMargin*2)/3
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        //设置
        layout.itemSize = CGSizeMake(cellWH, cellWH)
        // 设置垂直和水平间距
        layout.minimumInteritemSpacing = cellMargin
        layout.minimumLineSpacing = cellMargin
        //设置数据源代理
        dataSource = self
        delegate = self
        registerClass(SYComposePictureViewCell.self, forCellWithReuseIdentifier: SHComposePictureViewCellId)
    }
}
//   UICollectionViewDataSource 数据源代理方法
extension SYComposePictureView: UICollectionViewDataSource,UICollectionViewDelegate {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //为了 显示加号按钮
        let count = imageList.count
        //当 0 或 9 张 图片   根本不显示图片
        if count == 0 || count == 9{
            return count
        }
        //其他的 显示加号  所以+1
        return imageList.count + 1
        
    }
    func  collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(SHComposePictureViewCellId, forIndexPath: indexPath) as! SYComposePictureViewCell
//                cell.backgroundColor = RandomColor()
        //        cell.image = imageList[indexPath.item]
        
        // 判断是最后一个item 代表要显示加号图片
        if indexPath.item == imageList.count{
            //显示 加号
            cell.image = nil
            
        }else{
            //显示图片
            cell.image = imageList[indexPath.item]
            
        }
        
        //回调闭包 03
        cell.closure = {[weak self] in
        
        //移除指定位置的image
            self?.imageList.removeAtIndex(indexPath.item)
            //如果 没有图片 自动隐藏
            if self?.imageList.count == 0{
                
            self?.hidden = true
            }
            
            //刷新UI
            self?.reloadData()
        }
        
        
        return cell
    }
    
    //点击的图片 ((选择下的))
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        collectionView.deselectItemAtIndexPath(indexPath, animated: true)
        // 代表点击的为加号按钮
        if indexPath.item == imageList.count {
            closure?()
        }
    
}


//自定义cell
class SYComposePictureViewCell: UICollectionViewCell{
    
    //定义一个闭包 01
    var closure: (()->())?
    
    //定义一个属性供外界赋值
    var image: UIImage? {
        didSet{
            //添加加号图片
            if image == nil{
                bgImageView.image = UIImage(named: "compose_pic_add")
                //设置高亮图片
                bgImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                deleteButton.hidden = true
            }else{
                //添加图片
                bgImageView.image = image
                
                //(cell的复用)
                bgImageView.highlightedImage = nil
                deleteButton.hidden = false
            }
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK :  -设置视图
    private  func setupUI(){
        //添加控件
        contentView.addSubview(bgImageView)
        contentView.addSubview(deleteButton)
        //设置约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        deleteButton.snp_makeConstraints { (make) -> Void in
            make.top.trailing.equalTo(contentView)
        }
        
    }
    
    //MARK :  -懒加载控件
    //配图
    private lazy var bgImageView: UIImageView = UIImageView()
    //删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        //添加点击事件
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        
        return button
    }()
    //MARK :  -监听方法
    func buttonClick(){
    //执行 闭包 .02
    closure?()
    }
    
}

}



